////////////////////////////page_checkCar////////////////////////////
$(document).on("pageInit", "#page_checkCar", function(e, pageId, $page) {

    my.givetitle('验证车辆');
	key=my.get_key(my.getCookie('token'));
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
        }
    }
    if(theRequest['url'])
        $("#zc").attr("rel",theRequest['url']);
	var getCars=function(cars){
		       var list=cars

      		     var other_name=''
      		     var other_id=''
      		     $('.get-car-other').click(function(){
               	  $('.get-car-listb,.get-car-listc,.get-car-listd').hide()
               	  $('.get-car-other-box,.get-car-other-btn').show()
               	  other_name='[name='+$(this).attr('name')+'_name'
               	  other_id='[name='+$(this).attr('name')+'_id'
                })
                 $('.get-car-other-btn').click(function(){
                	$('.sel-car').text($('.get-car-other-box').val())
                	$(other_name).val($('.get-car-other-box').val())
                	$(other_id).val(0)
                	$.closeModal('.popup')
                })
	             $('.get-car-lista span').click(function(){
		           $('.get-car-lista span').removeClass('active')
		           $(this).addClass('active')
		           var name=$(this).attr('name')
		           $('.get-car-listb span').not('.get-car-other').remove()
		           $('.get-car-listb').show()
		           $('.get-car-listc,.get-car-listd,.get-car-other-box,.get-car-other-btn').hide()
		           for( k in list){
		           	if(list[k].brand_spell!=null&&name.match(list[k].brand_spell.substr(0,1))){
				         var html="<span id='"+k+"'>"+list[k].brand_name+"</span>"
				       $('.get-car-listb').prepend(html)
		             }
		           }
		           $('.get-car-listb span').not('.get-car-other').click(function(){
		           	      $('[name=brand_id]').val($(this).attr('id'))
		 	              $('.get-car-listb span').removeClass('active')
		                  $(this).addClass('active')
		                  $('.get-car-listc span').not('.get-car-other').remove()
		                  $('.get-car-listc').show()
		                  $('.get-car-listd span').not('.get-car-other').remove()
		                  $('.get-car-listd').hide()
		                  var j=$(this).attr('id')
		                   for(k in list[j].xi){
				               var html="<span id='"+j+"' name='"+k+"' >"+list[j].xi[k].cars_name+"</span>"
				                   $('.get-car-listc').prepend(html)
		                   }
		                   $('.get-car-listc span').not('.get-car-other').click(function(){
		                   	                    $('[name=cars_id]').val($(this).attr('name'))
		                   	                     $('.get-car-listc span').removeClass('active')
		                                         $(this).addClass('active')
		                                         $('.get-car-listd span').not('.get-car-other').remove()
		                                         $('.get-car-listd').show()
		                                         var j=$(this).attr('id')
		                                         var w=$(this).attr('name')
		                                         for(k in list[j].xi[w].chexing){
				                                       var html="<span id='"+k+"'>"+list[j].xi[w].chexing[k].models_name+"</span>"
				                                           $('.get-car-listd').prepend(html)
		                                            }
		                                          $('.get-car-listd span').not('.get-car-other').click(function(){
		                                          	$('[name=models_id]').val($(this).attr('id'))
                	                                $('.sel-car').text($('.get-car-listb span.active').text()+'-'+$('.get-car-listc span.active').text())
                	                                 $.closeModal('.popup')
                	                                 $('.popup-overlay,.modal-overlay').remove()
                                                  })
	                    })
	                })
                })
	             //选择品牌
      			$('.sel-car').click(function(){
      			   $.popup('.get-car');
      			  $('span[name=abc]').trigger('click')
      			})
	}

    my.getCars(getCars)
                $('.commit-state').click(function(){
                	if($(this).is(':checked')){
                		$('[name=commit_state]').val('1')
                	}else{
                		$('[name=commit_state]').val('0')
                	}
                	console.log($('[name=commit_state]').val())
                })

      			//上传图片
      			$('.img-xsz-z').click(function(){
      				 /*var list={"id":".img-xsz-z","text":"行驶证正面"}
      			         list=JSON.stringify(list)
      				 ad.native_get_photo(list)*/
                    var list={"id":".img-xsz-z","text":"行驶证正面"}
                    list=JSON.stringify(list)
                    //alert(list);
                    //showinmage(list);
                    var proc=function(){
                        showinmage(list);
                    };
                    callwechatselectimg(proc);
      			})
      			$('.img-xsz-f').click(function(){
                    var list={"id":".img-xsz-f","text":"行驶证反面"}
                    list=JSON.stringify(list)
                    //alert(list);
                    var proc=function(){
                        showinmage(list);
                    };
                    callwechatselectimg(proc);
      				 /*var list={"id":".img-xsz-f","text":"行驶证反面"}
      			         list=JSON.stringify(list)
      				 ad.native_get_photo(list)*/
      			})
//    			$('.img-jqx').click(function(){
//    				 var list={"id":".img-jqx","text":"交强险保单"}
//    			         list=JSON.stringify(list)
//    				 ad.native_get_photo(list)
//    			})
//    			$('.img-syx').click(function(){
//    				 var list={"id":".img-syx","text":"商业险保单"}
//    			         list=JSON.stringify(list)
//    				 ad.native_get_photo(list)
//    			})
      			//提交
      			$('#zc').click(function(){
                    if( !$('[name=checkCarsubtoauditnopark]').is(':checked')){
                        $.alert('请确认勾选"立即认证"');
                        return false;
                    };

      			    $('#zc').addClass('active_btn')
//    				if($('.img-xsz-z').attr('name')=='0'){$.toast('请上传行驶证正本照片'); $('#zc').removeClass('active_btn'); return false}
//    				if($('.img-xsz-f').attr('name')=='0'){$.toast('请上传行驶证副本照片'); $('#zc').removeClass('active_btn'); return false}
//    				if($('.img-jqx').attr('name')=='0'){$.toast('请上传交强险保单照片'); $('#zc').removeClass('active_btn'); return false}
//    				if($('.img-syx').attr('name')=='0'){$.toast('请上传商业险保单照片');$('#zc').removeClass('active_btn'); return false}
      				$.showPreloader()
      				var info_checkCar={
      						//'u_userid':sessionStorage.id,
      						'car_carid':0,
      						'u_userid':my.getCookie('id'),
      						'c_number':encodeURIComponent($('.numlocal-parkIndex label').text()+$('[name=c_number]').val()),
      						'veh_first_img':$('.img-xsz-z').attr('name'),
      						'veh_second_img':$('.img-xsz-f').attr('name'),
      						'commit_state':$('[name=commit_state]').val(),
      						'insurance_num':"",
      						'busin_num':"",
      						'insurance_img':"",
      						'busin_img':"",

      						'brand_id':encodeURIComponent($('[name=brand_id]').val()),
      						'brand_name':encodeURIComponent($('[name=brand_name]').val()),
      						'cars_id':encodeURIComponent($('[name=cars_id]').val()),
      						'cars_name':encodeURIComponent($('[name=cars_name]').val()),
      						'models_id':encodeURIComponent($('[name=models_id]').val()),
      						'models_name':encodeURIComponent($('[name=models_name]').val()),
      				}
      				console.log(info_checkCar)
    					info_checkCar=JSON.stringify(info_checkCar)
                        info_checkCar=my.cryptoASEencrypt(info_checkCar,key)

      				$.ajax({
      						type:"post",
      						url:api.usercarsmanage,
      						data:{
      							'api_value':info_checkCar
      						},
      						async:true,
      						error:function(){
      							$.hidePreloader()
      					       	$('#zc').removeClass('active_btn')
      							$.toast("提交失败，请重试..");
      						},
      						success:function(data){
      							$.hidePreloader()
      					       	$('#zc').removeClass('active_btn')
      							if(data.match("err_code")!=null){
      								$.hideIndicator()
      								switch(JSON.parse(data).err_code)
      								{
      									case 40003:
      									    my.tokenExpire()
      									   break;
      									default:
      									   $.alert(JSON.parse(data).err_msg);
      								}

      							}else{

      								    //data=JSON.parse(my.cryptoASEdencrypt(data,key))
        								//$.toast("提交成功，等待审核");

        								$.alert("您提交的车辆信息正在审核中，请保持手机畅通，我们将短信通知您审核结果!",function(){
                                            var k=$("#zc").attr("rel");
                                              if(k!=null && k!="")
                                                  $.router.load($("#zc").attr("rel"));
                                              else
                                                  $.router.load(carMangerUrl,true);
        									 //$.router.loadPage("carManger.html");

        								})

      							}
      						},
      				});

      			})
 /*popup选择车牌类型*****************************************************/
                   $('.numlocal-parkIndex').off('click').on('click',function(){
                        $.popup('.popup-carInfo-num');
                   })
                   $('.itemLocal-parkIndexGetCar span').off('click').on('click',function(){
                        $(this).addClass('active').siblings().removeClass('active')
                        $('.numlocal-parkIndex label').text($(this).text())
                   })
/*popup选择车牌类型*****************************************************/
})